package com.cndw.kungfu.model.goods.app;

import java.util.Map;

import com.cndw.kungfu.config.ConstantsIntKey;
import com.cndw.kungfu.config.ConstantsRadio;
import com.cndw.kungfu.exception.ErrPopException;
import com.cndw.kungfu.ext.LanguageLib;
import com.cndw.kungfu.vo.EventNotice;

/**
 * 使用激活某个跟随挂件 1 月兔 2 飞天 4 小狐精魄
 * @author fantadust
 *
 */
public class AppDo131 extends AppBase {

	@Override
	public boolean check() throws Exception {
		if (player.getDataInfo().intDataBitContains(ConstantsIntKey.g, goodsMode.getValueA())) {
			throw new ErrPopException(LanguageLib.get("goods.use.shoucang"));
		}
		if (goodsMode.getValueB() > 0 ) {
			int haveNum = player.getPlayerBoxs().getStackNum(goodsMode.getGoodsId());
			if (haveNum < goodsMode.getValueB()) {
				throw new ErrPopException(LanguageLib.get("goods.use.neednum",goodsMode.getValueB()));
			}
			
		}
		return true;
	}

	@Override
	public Map<String, Object> run() throws Exception {
		if (goodsMode.getValueB() > 0 ) {
			player.getPlayerBoxs().costGoods(goodsMode.getGoodsId(), goodsMode.getValueB() - 1);
		}
		int g = player.getDataInfo().intDataGet(ConstantsIntKey.g);
		g |= goodsMode.getValueA();
		player.getDataInfo().intDataSet(ConstantsIntKey.g, g);
		if (player.getDataInfo().intDataGet(ConstantsIntKey.gj) == 0) {//未使用的激活
			player.getDataInfo().intDataSet(ConstantsIntKey.gj, goodsMode.getValueA());
			EventNotice eventNotice = new EventNotice(ConstantsRadio.comm, ConstantsRadio.commGJChange);
			eventNotice.put("uid", player.getPlayerId());
			eventNotice.put("gj", goodsMode.getValueA());
			eventNotice.broadcastScene(player);
		}
		player.getDataInfo().commitSync();
		msg.put("gj", goodsMode.getValueA());
		
		return msg;
	}

}
